﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using ZG_member.BLL;

namespace ZG_member
{
    public partial class Dingdanxiugai : Form
    {
        public Dingdanxiugai()
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }
        ConsumptionBill cb;
        public Dingdanxiugai(int id,ConsumptionBill cpb) {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            cb = cpb;
            if (id == 1)
            {
                this.panel1.Visible = true;
                this.panel2.Visible = false;
            }
            if (id == 2)
            {
                this.panel1.Visible = false;
                this.panel2.Visible = true;

                DataSet ds = gibll.GetListWhere(" wasteBookId =" + cb.DgvConsumptionBill.CurrentRow.Cells["Column1"].Value.ToString());
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {

                    Model.goods gmodle = gbll.getModelGoodsIdString(ds.Tables[0].Rows[i]["gid"].ToString());
                    this.cboyigoumaideshangpin.Items.Add(gmodle.goodsName);
                    //this.lblshangpin.Text += "商品：";
                    //this.lblshangpin.Text += gmodle.goodsName;
                    //this.lblshangpin.Text += " 数量：";
                    //this.lblshangpin.Text += ds.Tables[0].Rows[i]["goodscount"].ToString() + " ";
                }

                List<Model.goodsType> list = gtbll.GetModelList("");
                this.cboshangpintype.DataSource = gtbll.GetModelList("");
                this.cboshangpintype.DisplayMember = "typename";
                this.cboshangpintype.ValueMember = "id";
                this.cboyigoumaideshangpin.SelectedIndex = 0;
                this.cboshangpintype.SelectedIndex = 0;
                jilian();
            }
            

        }


        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public string buypart(decimal price,string memberid)
        {
            Model.consumeSolution cm = new Model.consumeSolution();
            consumeSolution cs = new consumeSolution();
            memberInfo mi = new memberInfo();
            Model.memberInfo memberInfoMod = mi.GetUserIdMode(memberid);
            cm = cs.GetModel((int)memberInfoMod.cardType);
            string consumeSolution = cm.consumeSolution1;
            string[] one = consumeSolution.Split('|');
            string part = "0";
            int yushu = 0;
            if (price == 0)
            {
                return part;
            }

            for (int i = 0; yushu == 0; i++)
            {

                string[] two = one[i].Split(',');


                if (two[0] == "")
                {

                    break;
                }
                else
                {
                    if (price >= int.Parse(two[0]))
                    {

                        decimal beishu = price / int.Parse(two[0]);

                        part = (beishu * int.Parse(two[1])).ToString();
                    }
                }
            }

            birthdayremind br = new birthdayremind();
            Model.birthdayremind brmodl = br.GetModel(2);
            int aa = br.tixingday(memberid);
            string shengrijifen = brmodl.Birthdaydiscount;
            if (aa == 0)
            {

                //decimal beishu = price / int.Parse(shengrijifen[0]);

                part = (int.Parse(shengrijifen) * decimal.Parse(part)).ToString();
                return part;
            }


            return part;
        }
        memberInfo m = new memberInfo();
        private void btnjine_Click(object sender, EventArgs e)
        {
            if (this.txtkapian.Text.Trim() == "")
            {
                this.txtkapian.Text = "0";
            }
            if (this.txtxianjin.Text.Trim() == "")
            {
                this.txtxianjin.Text = "0";
            }
            Model.memberInfo memod = m.GetModels(cb.DgvConsumptionBill.CurrentRow.Cells["Column4"].Value.ToString());
            if (memod==null)
            {
                MessageBox.Show("修改失败，该会员以不存在！");
                return;
            }
            memod.Balance = memod.Balance + decimal.Parse(lblhong2.Text);
            memod.point = memod.point - int.Parse(cb.DgvConsumptionBill.CurrentRow.Cells["Column13"].Value.ToString());
            memod.consume = memod.consume - decimal.Parse(lblhong2.Text) - decimal.Parse(lblhong4.Text);

            cb.DgvConsumptionBill.CurrentRow.Cells["Column8"].Value = this.txtkapian.Text;
            cb.DgvConsumptionBill.CurrentRow.Cells["Column9"].Value = this.txtxianjin.Text;
            memod.Balance = memod.Balance - decimal.Parse(this.txtkapian.Text);
            decimal price = decimal.Parse(this.txtkapian.Text)+decimal.Parse(this.txtxianjin.Text);
            decimal jifen = decimal.Parse(buypart(price, cb.DgvConsumptionBill.CurrentRow.Cells["Column4"].Value.ToString()));
            memod.point = memod.point + int.Parse(jifen.ToString("F0"));
            memod.consume = memod.consume + decimal.Parse(txtkapian.Text) + decimal.Parse(txtxianjin.Text);
            if (m.Update(memod))
            {
                MessageBox.Show("修改成功！");
                this.Close();
            } 

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //timer开启
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //timer关闭
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;

        goodsInfo gibll = new goodsInfo();
        goodsType gtbll = new goodsType();
        goods gbll = new goods();
        private void Dingdanxiugai_Load(object sender, EventArgs e)
        {
            this.lblhong1.ForeColor = Color.Red;
            this.lblhong2.ForeColor = Color.Red;
            this.lblhong3.ForeColor = Color.Red;
            this.lblhong4.ForeColor = Color.Red;
            //this.lblshangpin.ForeColor = Color.Red;
            this.lblhong2.Text = cb.DgvConsumptionBill.CurrentRow.Cells["Column8"].Value.ToString();
            this.lblhong4.Text = cb.DgvConsumptionBill.CurrentRow.Cells["Column9"].Value.ToString();
            this.Opacity = 0;
            this.timer1.Start();
            
        }
        public void jilian()
        {
            goods g = new goods();
            try
            {
                List<Model.goods> gg = g.GetModelList("goodstypeid = " + this.cboshangpintype.SelectedValue);
                if (gg.Count <= 0)
                {
                    MessageBox.Show("没有该分类的商品！");
                    return;
                }
                this.cboshangpinname.DataSource = gg;
            }
            catch (Exception)
            {

                return;
            }

            this.cboshangpinname.DisplayMember = "goodsName";
            this.cboshangpinname.ValueMember = "goodsId";
        }
        private void Dingdanxiugai_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void btnshangpingquxiao_Click(object sender, EventArgs e)
        {
            this.Close();
        } 

        private void btnshangpinxiugai_Click(object sender, EventArgs e)
        {
            Model.goods gmod = gbll.GetModelName(this.cboyigoumaideshangpin.Text);
            if (gmod==null)
            {
                return;
            }

            DataSet ds = gibll.GetListWhere(" wasteBookId =" + cb.DgvConsumptionBill.CurrentRow.Cells["Column1"].Value.ToString() + " and gid = " + gmod.goodsId);
            if (ds.Tables[0].Rows.Count == 0)
            {
                return;
            }
            BLL.memberInfo mbll = new memberInfo();
            Model.goodsInfo gimod = new Model.goodsInfo();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                gmod.amount = gmod.amount + int.Parse(ds.Tables[0].Rows[i]["goodscount"].ToString());
                if ( gbll.Update(gmod))
                {
                    Model.memberInfo mmod = mbll.GetModels(cb.DgvConsumptionBill.CurrentRow.Cells["Column4"].Value.ToString());
                    if (mmod == null)
                    {
                        MessageBox.Show("该会员已被删除！");
                        return;
                    }
                    mmod.consume = mmod.consume - decimal.Parse(cb.DgvConsumptionBill.CurrentRow.Cells["Column7"].Value.ToString());
                    mmod.Balance = mmod.Balance + decimal.Parse(cb.DgvConsumptionBill.CurrentRow.Cells["Column7"].Value.ToString());
                    decimal jifen = decimal.Parse(buypart(decimal.Parse(cb.DgvConsumptionBill.CurrentRow.Cells["Column7"].Value.ToString()), cb.DgvConsumptionBill.CurrentRow.Cells["Column4"].Value.ToString()));
                    mmod.point = mmod.point - int.Parse(jifen.ToString("F0"));
                    if (mbll.Update(mmod))
                    {
                        Model.goods g = gbll.GetModelName(this.cboshangpinname.Text);

                        gimod.Id = int.Parse(ds.Tables[0].Rows[i]["id"].ToString());
                        gimod.Gid = this.cboshangpinname.SelectedValue.ToString();
                        gimod.Goodscount = int.Parse(ds.Tables[0].Rows[i]["goodscount"].ToString());
                        gimod.Goodsprice = (decimal)g.price;
                        gimod.Selltime = DateTime.Now.ToString();
                        gimod.Zhekou = ds.Tables[0].Rows[i]["zhekou"].ToString();
                        gimod.wasteBookId = int.Parse(ds.Tables[0].Rows[i]["wasteBookId"].ToString());
                        if (gibll.Update(gimod))
                        {
                            mmod.consume = mmod.consume + int.Parse(ds.Tables[0].Rows[i]["goodscount"].ToString()) * (decimal)g.price * (10/decimal.Parse(ds.Tables[0].Rows[i]["zhekou"].ToString()));
                            mmod.Balance = mmod.Balance - int.Parse(ds.Tables[0].Rows[i]["goodscount"].ToString()) * (decimal)g.price * (10 / decimal.Parse(ds.Tables[0].Rows[i]["zhekou"].ToString()));
                            jifen = decimal.Parse(buypart(int.Parse(ds.Tables[0].Rows[i]["goodscount"].ToString()) * (decimal)g.price * (10 / decimal.Parse(ds.Tables[0].Rows[i]["zhekou"].ToString())), cb.DgvConsumptionBill.CurrentRow.Cells["Column4"].Value.ToString()));
                            mmod.point = mmod.point + int.Parse(jifen.ToString("F0"));
                            if (mbll.Update(mmod))
                            {
                                wasteBook wbll = new wasteBook();
                                Model.wasteBook wmod = wbll.GetModel(int.Parse(cb.DgvConsumptionBill.CurrentRow.Cells["Column1"].Value.ToString()));
                                wmod.consumeMoney = int.Parse(ds.Tables[0].Rows[i]["goodscount"].ToString()) * (decimal)g.price * (10 / decimal.Parse(ds.Tables[0].Rows[i]["zhekou"].ToString()));

                                cb.DgvConsumptionBill.CurrentRow.Cells["Column7"].Value = int.Parse(ds.Tables[0].Rows[i]["goodscount"].ToString()) * (decimal)g.price * (10 / decimal.Parse(ds.Tables[0].Rows[i]["zhekou"].ToString()));
                                if (mmod.Balance >= int.Parse(ds.Tables[0].Rows[i]["goodscount"].ToString()) * (decimal)g.price * (10 / decimal.Parse(ds.Tables[0].Rows[i]["zhekou"].ToString())))
                                {
                                    wmod.cardPayMoney = int.Parse(ds.Tables[0].Rows[i]["goodscount"].ToString()) * (decimal)g.price * (10 / decimal.Parse(ds.Tables[0].Rows[i]["zhekou"].ToString()));
                                    wmod.cashPayMoney = 0;
                                    cb.DgvConsumptionBill.CurrentRow.Cells["Column8"].Value = int.Parse(ds.Tables[0].Rows[i]["goodscount"].ToString()) * (decimal)g.price * (10 / decimal.Parse(ds.Tables[0].Rows[i]["zhekou"].ToString()));
                                    cb.DgvConsumptionBill.CurrentRow.Cells["Column9"].Value = 0;
                                }
                                else
                                {
                                    decimal jiage = int.Parse(ds.Tables[0].Rows[i]["goodscount"].ToString()) * (decimal)g.price * (10 / decimal.Parse(ds.Tables[0].Rows[i]["zhekou"].ToString()));
                                    cb.DgvConsumptionBill.CurrentRow.Cells["Column8"].Value = mmod.Balance;
                                    cb.DgvConsumptionBill.CurrentRow.Cells["Column9"].Value = jiage - mmod.Balance;
                                    wmod.cardPayMoney = mmod.Balance;
                                    wmod.cashPayMoney = jiage - mmod.Balance;

                                }
                                
                                cb.DgvConsumptionBill.CurrentRow.Cells["Column13"].Value = int.Parse(jifen.ToString("F0"));
                                cb.DgvConsumptionBill.CurrentRow.Cells["Column14"].Value = mmod.point;
                                wmod.exchangeIntegral = int.Parse(jifen.ToString("F0"));
                                wmod.totalIntegral = mmod.point;
                                if (wbll.Update(wmod))
                                {
                                    MessageBox.Show("修改成功！");
                                    this.Close();
                                } 

                                
                                
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("修改失败！");
                }

            }
           

        }

        private void cboyigoumaideshangpin_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        
    }
}
